package com.android.Lmaps;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;

public class AddNewLocation extends Activity {

	EditText txtAddress, txtPrice,txtDescription,txtContact;
	TextView lblAddress, lblPrice,lblDescription,lblContact;
	Button btnOk, btnCancel, btnRefesh;
	CheckBox chkShare;
	//__________________________________________________________________________
	Boolean flagAddress =false,flagContact =false,flagDescription =false; 
	//__________________________________________________________________________
	Intent mainIntent;
	Bundle mydata;
	//__________________________________________________________________________
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_new_location);
		
		txtAddress = (EditText)findViewById(R.id.editText_add_address);
		txtContact= (EditText)findViewById(R.id.editText_add_contact);
		txtDescription= (EditText)findViewById(R.id.editText_add_description);
		txtPrice= (EditText)findViewById(R.id.editText_add_price);
		
		lblAddress =(TextView)findViewById(R.id.textView_add_address);
		lblContact=(TextView)findViewById(R.id.textView_add_contact);
		lblDescription=(TextView)findViewById(R.id.textView_add_description);
		lblPrice=(TextView)findViewById(R.id.textView_add_price);
		
		btnOk = (Button)findViewById(R.id.button_add_ok);
		btnCancel =(Button)findViewById(R.id.button_add_cancel);
		btnRefesh=(Button)findViewById(R.id.button_add_refesh);
		
		chkShare=(CheckBox)findViewById(R.id.checkBox_add_share);
		Log.d("Edit	add_", "1");
		
		//___________________________________________________________________________
		lblAddress.setBackgroundColor(Color.YELLOW);
		lblContact.setBackgroundColor(Color.YELLOW);
		lblDescription.setBackgroundColor(Color.YELLOW);
		//__________________________________________________________________________
		mainIntent = getIntent();
		mydata = mainIntent.getExtras();
		if(mydata.getBoolean("stype"))//add
		{
			//do nothing
		}
		else//edit
		{
			//show to edit
			Log.d("Edit	add_", "2");
			txtAddress.setText(mydata.getString("address"));
			Log.d("Edit	add_", "3");
			Double pric =mydata.getDouble("price");
			Log.d("Edit	add_", "4");
			if(pric==-1.0)
				txtPrice.setText("");
			else{
				txtPrice.setText(""+pric.toString());
			}
			Log.d("Edit	add_", "5");
			
			txtContact.setText(mydata.getString("contact"));
			txtDescription.setText(mydata.getString("description"));//description
			
			Log.d("Edit	add_", "6");
			if(mydata.getBoolean("personshare"))
			{
				chkShare.setChecked(true);
			}
			else
			{
				chkShare.setChecked(false);
			}
		}
		//__________________________________________________________________________
		//__________________________________________________________________________
		
		txtAddress.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if(s.length()<=0)
				{
					flagAddress=false;
					lblAddress.setBackgroundColor(Color.YELLOW);
				}
				else
				{
					flagAddress=true;
					lblAddress.setBackgroundColor(Color.GREEN);
				}
			}
		});
		txtContact.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if(s.length()<=0)
				{
					flagContact=false;
					lblContact.setBackgroundColor(Color.YELLOW);
				}
				else
				{
					flagContact=true;
					lblContact.setBackgroundColor(Color.GREEN);
				}
			}
		});
		txtDescription.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if(s.length()<=0)
				{
					flagDescription=false;
					lblDescription.setBackgroundColor(Color.YELLOW);
				}
				else
				{
					flagDescription=true;
					lblDescription.setBackgroundColor(Color.GREEN);
				}
			}
		});
		//__________________________________________________________________________
		btnOk.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.d("add new", "1");
				if(!checkOk())
				{
					Toast.makeText(getApplicationContext(), "Please insert on the form!", Toast.LENGTH_LONG).show();
					return;
				}
				Log.d("add new", "2");
				mydata.putBoolean("personshare",chkShare.isChecked());
				mydata.putString("address",txtAddress.getText().toString());
				mydata.putString("contact",txtContact.getText().toString());
				mydata.putString("description", txtDescription.getText().toString());
				Double pric=0.0;
				if(txtPrice.getText().length()<=0)
					pric=-1.0;
				try {
					pric = Double.parseDouble(txtPrice.getText().toString());
				} catch (Exception e) {
					// TODO: handle exception
					pric=-1.0;
				}
				Log.d("add new", "3");
				mydata.putDouble("price",pric);
				mainIntent.putExtras(mydata);
				Log.d("add new", "4");
				setResult(Activity.RESULT_OK, mainIntent);
				finish();
				
			}
		});
		btnCancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setResult(Activity.RESULT_CANCELED, mainIntent);
				finish();
			}
		});
		btnRefesh.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				txtAddress.setText("");
				txtContact.setText("");
				txtDescription.setText("");
				txtPrice.setText("");
			}
		});
		//__________________________________________________________________________
	}
	
	public Boolean checkOk() {
		
		if(!flagAddress)
			return false;
		if(!flagContact)
			return false;
		if(!flagDescription)
			return false;
		return true;
	}

}
